package com.jkc.xh.fragments;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.activities.LoginActivity_;
import com.jkc.xh.activities.WebActivity_;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BaseFragment;
import com.jkc.xh.manager.UserManager;
import com.jkc.xh.models.UserInfo;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.utils.CacheTools;
import com.jkc.xh.utils.Tool;
import com.jkc.xh.views.widget.NavBar;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * Created by zhangbreeze on 15/11/21.
 */
@EFragment(R.layout.fragment_account)
public class AccountFragment extends BaseFragment {
    @ViewById
    NavBar nav_bar;
    @ViewById(R.id.dta_money)
    TextView dta_money;
    @ViewById(R.id.bean_money)
    TextView bean_money;
    @ViewById
    TextView userName;
    @ViewById
    TextView message_count;
    @ViewById
    TextView redpackCount;
    @ViewById
    TextView score;
    @ViewById
    TextView lev;
    @ViewById
    TextView score_remain;
    @ViewById
    TextView tvVersionName;

    @AfterViews
    void initView() {
        nav_bar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onRightMenuClick(View view) {
                loadUserInfo();
            }
        });
        loadUserInfo();
        //tvVersionName.setText(getString(R.string.version_name, Tool.getAppVersion(getContext(), false)));
        tvVersionName.setText(getString(R.string.version_name, "5.0"));
    }

    private void loadUserInfo() {
        showProgress("正在加载...");
        addJob(UserManager.getUserInfo(new RestCallBack<UserInfo>() {
            @Override
            public void success(UserInfo userInfo) {
                userName.setText(userInfo.userName + "");
                message_count.setText(getString(R.string.str_unread_message, userInfo.messageCount));
                redpackCount.setText(getString(R.string.str_redpack_count, userInfo.redpackCount));
                score.setText(Tool.formatNumNoScale(userInfo.score));
                lev.setText(userInfo.lev);
                score_remain.setText(getString(R.string.str_score_remain, Tool.formatNumNoScale(userInfo.scoreRemain)));
                dta_money.setText(Tool.formatPrice(userInfo.money));
                bean_money.setText(Tool.formatPrice(userInfo.beanMoney));
                hideProgress();
            }

            @Override
            public void failure(RestError error) {
                hideProgress();
            }
        }));

    }

    @Click(R.id.btnLogout)
    public void logout() {
        UserManager.clearLoginInfo();
        startActivity(new Intent(getContext(), LoginActivity_.class));
        getActivity().finish();
    }

    /**
     * 账户设置
     */
    @Click(R.id.toAccountSet)
    public void toAccountSet() {
        AccountSetFragment_.builder().build().startFragment(getContext());
    }

    /**
     * 密码设置
     */
    @Click(R.id.toChangePwdView)
    public void toChangePwd() {
        ChangePwdFragment_.builder().build().startFragment(getContext());
    }

    /**
     * 充值
     */
    @Click({R.id.btnRechargeMoney})
    void toRecharge() {
        PayWayFragment_.builder().build().startFragment(getContext());
    }

    /**
     * 我要提现
     */
    @Click({R.id.btnRefundCash})
    void toRefundCash() {
        PayRefundCashFragment_.builder().build().startFragment(getContext());
    }

    /**
     * 我要提现
     */
    @Click(R.id.function_ask_friend)
    void toAskFriend() {
        InviteFriendFragment_.builder().build().startFragment(getContext());
    }

    /**
     * 我的红包
     */
    @Click(R.id.toMyRedbagView)
    void toMyRedbagView() {
        RedbagFragment_.builder().build().startFragment(getContext());
    }

    /**
     * 抵用券记录
     */
    @Click(R.id.toMyMagicBeanView)
    void toMyMagicBeanView() {
        MagicBeanFragment_.builder().build().startFragment(getContext());
    }

    /**
     * 站内消息
     */
    @Click(R.id.toMyMessage)
    void toMyMessage() {
        MessageFragment_.builder().build().startFragment(getContext());
    }

    /**
     * 添加银行卡
     */
    @Click(R.id.toAddBankCardView)
    void toAddBankCard() {
        AddCreditCradFragment_.builder().build().startFragment(getContext());
    }

//    /**
//     * 推广明细
//     */
//    @Click(R.id.function_to_reffer)
//    void toRefferFriendList() {
//        RefferFriendFragment_.builder().build().startFragment(getContext());
//    }

    public static final String WEBSITE_USER_LEVEL = "/Index/Nest/rank/id/{sid}.html";

    /**
     * 积分等级
     */
    @Click(R.id.function_user_level)
    void toUserLevel() {
        String url = CacheTools.getPageUrl() + WEBSITE_USER_LEVEL.replace("{sid}", UserManager.getSid());
        Intent intent = new Intent(getContext(), WebActivity_.class);
        intent.putExtra(Constant.ACTION_AD_URL, url);
        intent.putExtra(Constant.ACTION_AD_TITLE, "会员等级");
        getContext().startActivity(intent);
    }
}
